<template>
  <div class="search-history">
    <!-- <p>搜索历史</p> -->
    <van-cell title="搜索历史">
      <div v-show="isDeleteShow">
        <span @click="$emit('clear-search-histories')">全部删除</span>
        &nbsp;&nbsp;
        <span @click="isDeleteShow = false">完成</span>
      </div>

      <van-icon
        v-show="!isDeleteShow"
        name="delete"
        @click="isDeleteShow = true"
      />
    </van-cell>
    <van-cell
      v-for="(item, index) in searchHistories"
      :key="index"
      :title="item"
      @click="onSearchItemClick(item, index)"
    >
      <template>
        <van-icon v-show="isDeleteShow" name="close" />
      </template>
    </van-cell>
  </div>
</template>

<script>
export default {
  name: 'SearchHistory',
  props: {
    // 搜索记录
    searchHistories: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      // 控制显示删除按钮
      isDeleteShow: false
    };
  },
  computed: {},
  created() {},
  methods: {
    /**
     * 历史记录 item 点击事件
     */
    onSearchItemClick(item, index) {
      if (this.isDeleteShow) {
        // 删除状态，删除历史记录数据
        this.searchHistories.splice(index, 1);
      } else {
        // 非删除状态，直接进入搜索
        this.$emit('search', item);
      }
    }
  }
};
</script>

<style scoped lang="less"></style>
